<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14
 * Time: 14:32
 */
namespace App\Http\Controllers\Manage;

use App\Model\Affix;
use App\Model\Annex;
//use Log;
use Crypt;
use App\Model\ServiceProvider;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use OSS\OssClient;
use OSS\Core\OssException;

class ServiceProviderController extends Controller {
    public function serviceProviderList() {
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new ServiceProvider();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $service_provider = $obj->Paginate(20);
        if(empty($page)){
            $count = $obj->count();
        }

        return view('manage.serviceProvider.serviceProviderList',[
            'service_provider'=>$service_provider,
            'count' => $count,
            'orderBy' => $orderBy,
            'active1' => 15,
            'active2' => 1]);
    }

    public function serviceProviderAdd() {
        return view('manage.serviceProvider.serviceProviderAdd', ['active1' => 15, 'active2' => 2]);
    }

    public function serviceProviderCreate() {
        $rank = input::get('rank');
        $name = input::get('name');
        $phone = input::get('phone');
        $contact = input::get('contact');
        if(empty($name) || empty($phone) || empty($contact)) {
            return [
                'code' => 102,
                'msg' => '缺少参数!'
            ];
        }
        $repetition = ServiceProvider::where('name',$name)->first();
        if($repetition) {
            return [
                'code' => 103,
                'msg' => "该服务商已存在!"
            ];
        }
        $map = ServiceProvider::insert([
            'name' => $name,
            'rank' => $rank,
            'phone' => $phone,
            'contact' => $contact,
        ]);

        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'添加失败!'
            ];
        }


    }

    public function serviceProviderEdit($id) {
        $serviceProvider_id = Crypt::decrypt($id);
        $serviceProvider = ServiceProvider::find($serviceProvider_id);
        if (!empty($serviceProvider)) {
            return view('manage.serviceProvider.serviceProviderEdit', ['serviceProvider' => $serviceProvider, 'active1' => 15, 'active2' => 1]);
        }
        else {

        }
    }

    public function serviceProviderUpdate() {
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['name'] = input::get('name');
        $data['contact'] = input::get('contact');
        $data['phone'] = input::get('phone');
        if(empty($data['name'])) {
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = ServiceProvider::where('id',$id)->update($data);
        if($update) {
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }

    public function serviceProviderDelete(){
        $id = Input::get('id');
        $delete = ServiceProvider::where('id',$id)->delete();
        if($delete){
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }

    public function serviceProviderStatus() {
        $id = Input::get('id');
        $status = Input::get('status');

        if($status == 1) {
            $del = ServiceProvider::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = ServiceProvider::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    private function labelCreate($label) {
        Log::info('label here');
    }
}